package tpsi.pong;

import java.io.IOException;

import org.xlightweb.IWebSocketConnection;
import org.xlightweb.TextMessage;

public class Player {
	private String name; // players name
	private final long playerID; // players session ID
	private IWebSocketConnection conection; // players connection
	private boolean disconnected;
	private long timeout;
	private Game currentGame;
	private GameState state;

	public Player(long ID) {
		this.setName(null);
		this.playerID = ID;
		this.setConection(null);
		this.setDisconnected(false);
		this.setTimeout(-1);
		this.setCurrentGame(null);
		this.setState(GameState.NONE);
	}

	public Player(long ID, IWebSocketConnection conection) {
		this(ID);
		this.setConection(conection);
	}

	public Player(String name, long ID, IWebSocketConnection conection) {
		this(ID);
		this.setName(name);
		this.setConection(conection);
	}

	/**
	 * Checks, if this Player is pending for deletion.
	 * 
	 * @param currentTime
	 *            Current system time.
	 * @return True, if this Player should be deleted.
	 */
	public boolean forDeletion(long currentTime) {
		if (this.disconnected && (this.timeout > 0))
			if (this.timeout < currentTime)
				return true;
		return false;
	}

	public long getPlayerID() {
		return this.playerID;
	}

	public void setName(String name) {
		if (this.name == null)
			this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setConection(IWebSocketConnection conection) {
		this.conection = conection;
	}

	public IWebSocketConnection getConection() {
		return conection;
	}

	public void setDisconnected(boolean disconnected) {
		this.disconnected = disconnected;
	}

	public boolean isDisconnected() {
		return disconnected;
	}

	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}

	public long getTimeout() {
		return timeout;
	}

	public void setCurrentGame(Game currentGame) {
		this.currentGame = currentGame;
	}

	public Game getCurrentGame() {
		return currentGame;
	}

	/**
	 * Builds and return PLAYER message.
	 * 
	 * @return String replay message.
	 */
	public String getPlayerMsg() {
		StringBuilder sb = new StringBuilder();
		sb.append("PLAYER\n");
		sb.append(this.name);
		sb.append(":");
		sb.append(String.valueOf(this.playerID));
		return sb.toString();
	}

	/**
	 * Builds and return CID message.
	 * 
	 * @return String replay message.
	 */
	public String getCIDMsg() {
		StringBuilder sb = new StringBuilder();
		sb.append("CID\n");
		sb.append(String.valueOf(this.playerID));
		return sb.toString();
	}

	public void sendMsg(String msg) {
		if ((this.conection == null) || (msg == null))
			return;
		TextMessage tm = new TextMessage(msg);
		try {
			this.conection.writeMessage(tm);
			if (this.disconnected) {
				this.disconnected = false;
				this.timeout = -1;
			}
		} catch (IOException ex) {
			if (!this.disconnected) {
				this.disconnected = true;
				this.timeout = System.currentTimeMillis() + 60000;
			}
		}
	}

	public void setState(GameState state) {
		this.state = state;
	}

	public GameState getState() {
		return state;
	}
}
